使用 "else" 和 "else if" 语句

之前,我们使用了多个 if 语句来实现游戏规则。但是,在结束上一单元时,我们仍有改进的机会。我们可以对代码表达能力进行改进,并对修复代码中细微 bug 的能力进行改进。

通过添加 else 和 else if 语句来改进代码并修复 bug

接下来,我们将使用 if 语句的变体来改进代码并修复一个逻辑 bug。

使用 if 和 else 语句,而不是两个单独的 if 语句

不是执行两项检查来显示消息 " 你获胜了!" 或 " 抱歉,你失败了 ",而是将使用 else 关键字。修改代码以匹配以下代码清单:

Random dice = new Random();

int roll1 = dice.Next(1, 7);
int roll2 = dice.Next(1, 7);
int roll3 = dice.Next(1, 7);

int total = roll1 + roll2 + roll3;

Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");

if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
{
    Console.WriteLine("You rolled doubles!  +2 bonus to total!");
    total += 2;
}

if ((roll1 == roll2) && (roll2 == roll3))
{
    Console.WriteLine("You rolled triples!  +6 bonus to total!");
    total += 6;
}

if (total >= 15)
{
    Console.WriteLine("You win!");
}
else
{
    Console.WriteLine("Sorry, you lose.");
}

此处,如果 total >= 15 为 false,则将执行 else 关键字后面的代码块。由于这两个选项是相对的,因此此方案完美演示了何时适合使用 else 关键字。

使用嵌套消除双倍奖励和三倍奖励叠加的情形

在上个单元中,我们观察到了我们是怎样向应用程序中引入了一个细微的逻辑 bug 的。让我们使用嵌套来修复该 bug。

通过嵌套,我们可以将代码块放在代码块中。在此情况下,我们将 ifelse 语句(检查是否有三倍奖励)嵌套在另一个 if 语句(检查是否有两倍奖励)中,以防止两种情况同时发生。

我们将在对双倍奖励的检查中嵌套对三倍奖励的检查。修改代码以匹配以下代码清单:

int roll1 = 6;
int roll2 = 6;
int roll3 = 6;

int total = roll1 + roll2 + roll3;

Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");

if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
{
    if ((roll1 == roll2) && (roll2 == roll3))
    {
        Console.WriteLine("You rolled triples!  +6 bonus to total!");
        total += 6;
    }
    else
    {
        Console.WriteLine("You rolled doubles!  +2 bonus to total!");
        total += 2;
    }
}

if (total >= 15)
{
    Console.WriteLine("You win!");
}
else
{
    Console.WriteLine("Sorry, you lose.");
}

要在无需运行应用程序几十次的情况下测试代码,可以通过在声明和初始化 total 的行之前添加以下代码,临时对三个 roll 变量的值进行硬编码。

测试是否采用双倍奖励:

roll1 = 6;
roll2 = 6;
roll3 = 5;

// Dice roll: 6 + 6 + 5 = 17
// You rolled doubles!  +2 bonus to total!
// You win!

若要测试是否采用三倍奖励,请修改 roll3 的值:

roll1 = 6;
roll2 = 6;
roll3 = 6;

// Dice roll: 6 + 6 + 6 = 18
// You rolled triples!  +6 bonus to total!
// You win!

使用 if、else 和 else if 语句提供奖励,而不是显示胜败消息

为了使游戏更有趣,我们将游戏从输赢结果改为对每个分数奖励虚拟奖品。我们将提供 4 个奖品。玩家应该只赢取一个奖品:

针对以下代码清单修改前面步骤中的代码:

Random dice = new Random();

int roll1 = dice.Next(1, 7);
int roll2 = dice.Next(1, 7);
int roll3 = dice.Next(1, 7);

int total = roll1 + roll2 + roll3;

Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");

if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
{
    if ((roll1 == roll2) && (roll2 == roll3))
    {
        Console.WriteLine("You rolled triples!  +6 bonus to total!");
        total += 6;
    }
    else
    {
        Console.WriteLine("You rolled doubles!  +2 bonus to total!");
        total += 2;
    }
}

if (total >= 16)
{
    Console.WriteLine("You win a new car!");
}
else if (total >= 10)
{
    Console.WriteLine("You win a new laptop!");
}
else if (total == 7)
{
    Console.WriteLine("You win a trip for two!");
}
else
{
    Console.WriteLine("You win a kitten!");
}
备注

使用 roll 变量临时硬编码的技术来测试每条消息。

可以使用 ifelseelse if 语句创建多个排他性条件作为布尔表达式。换言之,如果你只希望产生一种结果,但却有多个可能的条件和结果,则根据需要使用任意数量的 else if 语句。如果 ifelse if 语句均不适用,则将执行最后的 else 代码块。 else 是可选的,但必须最后执行。

回顾